const { defineConfig } = require('cypress')
const mysql = require('mysql')
// const selectTestsWithGrep = require('cypress-select-tests/grep')

module.exports = defineConfig({
  e2e: {
    projectId: 'kiszeg',
    baseUrl: 'https://test-admin-dev.therol.cn',
    specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
    viewportWidth: 2560,
    viewportHeight: 1440,
    retries: {
      runMode: 1,
      openMode: 1,
    },
    defaultCommandTimeout: 15000,
    pageLoadTimeout: 60000,
    requestTimeout: 15000,
    responseTimeout: 60000,
    fixturesFolder: 'cypress/fixtures',
    downloadsFolder: 'cypress/downloads/',
    screenshotsFolder: 'cypress/screenshots',
    videosFolder: 'cypress/videos',
    trashAssetsBeforeRuns: true,
    video: true,
    reporter: 'junit',
    reporterOptions: {
      mochaFile: '../live cypress run/allure-results/test_report_[hash].xml',
      toConsole: true,
    },
    setupNodeEvents(on, config) {
      config.db = {
        test: {
          host: '43.138.225.183',
          user: 'root',
          password: 'rootpwd123',
        },
        pre: {
          host: '43.138.168.252',
          user: 'root',
          password: 'rootpwd123',
        },
      }

      const con = mysql.createConnection(config.db.test)
      on('task', {
        executeSql(sql) {
          return new Promise((resolve, reject) => {
            con.query(sql, (err, results) => {
              if (err) {
                reject(err.message)
                return
              }
              resolve(results)
            })
            // con.end()
          })
        },
      })
    },
    // setupNodeEvents(on, config) {
    //   on('file:preprocessor', selectTestsWithGrep(config))
    // },
  },
  env: {
    // sport_center: '罗湖体育中心',
    // anchor: '小鸣',
  },
})
